﻿(function ($) {

	if (typeof $.cef == 'undefined')
		$.cef = {};

	$.cef.invoke = function (method, params, success, failure) {
		if (typeof window.cefQuery == 'undefined') {
			failure(-3, '$.cef.invoke只能在Cef容器环境下工作.');
			return;
		}
			
		var reqJSON = {
			method: method,
			params: params
		};
				 
		window.cefQuery({
			request: JSON.stringify(reqJSON),
			onSuccess: function (response) {
				if (typeof success !== 'function')
					return;
				var respJSON = JSON.parse(response);
				if (respJSON == null)
					failure(-1, 'Unknown response');
				else if (!respJSON.success)
					failure(-2, 'Cef back end failure: ' + (respJSON.message || '[NULL]'));
				else
					success(respJSON.result);
			},
			onFailure: function (errCode, errMessage) {
				failure(-3, 'Channel failure: [' + errCode + '] ' + errMessage);
			}
		});

	};
	
})(jQuery);